package com.rd.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rd.mapper.MenuMapper;
import com.rd.pojo.Admin;
import com.rd.pojo.Menu;
import com.rd.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiufenfen
 * @since 2023-10-26
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public List<Menu> getMenusByAdminId() {
        List<Menu> list = null;
        //获取认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //认证成功的用户
        Admin admin = (Admin) authentication.getPrincipal();
        //用户id
        Integer userId = admin.getId();
        // 1. 先从redis缓存中获取 key是 menu_用户id
        Map<String,String> mapFromRedis =redisTemplate.opsForHash().entries("menu_" + userId);
        if(mapFromRedis!=null && mapFromRedis.size()>0) {
            log.info("redis缓存有对应数据,直接返回");
            list = mapFromRedis.values().stream().
                    map(str-> JSONUtil.toBean(str,Menu.class)).collect(Collectors.toList());
        }else{
            log.info("redis缓存中没有对应的数据,从数据库中查询");
            list = menuMapper.getMenusByAdminId(userId);
            //把从数据库中查询出来的数据缓存到redis中
            //操作Hash值
            Map<String,String> temp =new HashMap<>();
            //遍历list集合,将数据存入到temp集合中
            list.forEach(user->{temp.put(String.valueOf(user.getId()),JSONUtil.toJsonStr(user));});
            redisTemplate.opsForHash().putAll("menu_" + userId,temp);
        }
        return list;
    }

    @Override
    public List<Menu> getMenusByAdminId(Integer userId) {
        List<Menu> list = null;
        Map<String,String> mapFromRedis =redisTemplate.opsForHash().entries("menu_" + userId);
        if(mapFromRedis!=null && mapFromRedis.size()>0) {
            log.info("redis缓存有对应数据,直接返回");
            list = mapFromRedis.values().stream().
                    map(str-> JSONUtil.toBean(str,Menu.class)).collect(Collectors.toList());
        }else{
            log.info("redis缓存中没有对应的数据,从数据库中查询");
            list = menuMapper.getMenusByAdminId(userId);
            //把从数据库中查询出来的数据缓存到redis中
            //操作Hash值
            Map<String,String> temp =new HashMap<>();
            //遍历list集合,将数据存入到temp集合中
            list.forEach(user->{temp.put(String.valueOf(user.getId()),JSONUtil.toJsonStr(user));});
            redisTemplate.opsForHash().putAll("menu_" + userId,temp);
        }
        return list;
    }



    /**
     * 获取所有权限的同时获取角色
     * @return
     */
    @Override
    public List<Menu> getAllMenusWithRole() {
        return menuMapper.getAllMenusWithRole();
    }

    /**
     * @return
     * @description: 查询所有菜单
     * @param:
     */
    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }


}
